package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.dao.mapper.ChargingFailMapper;
import cn.tedu.charging.order.dao.mapper.ChargingSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.dto.OrderMqDTO;
import cn.tedu.charging.order.pojo.po.ChargingFailPO;
import cn.tedu.charging.order.pojo.po.ChargingSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired
    private ChargingSuccessMapper chargingSuccessMapper;
    @Autowired
    private ChargingFailMapper chargingFailMapper;



    @Override
    public void updateOrderStatusFromProcess2Exception(OrderMqDTO orderMqDTO) {
        ChargingSuccessPO chargingSuccessPO = new ChargingSuccessPO();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bill_status",1);
        updateWrapper.eq("bill_id",orderMqDTO.getOrderNo());
        chargingSuccessPO.setBillStatus(3);
        chargingSuccessMapper.update(chargingSuccessPO,updateWrapper);
    }

    @Override
    public ChargingSuccessPO getSuccessOrder(String orderNo) {
        QueryWrapper<ChargingSuccessPO> chargingSuccessPOQueryWrapper = new QueryWrapper<>();
        chargingSuccessPOQueryWrapper.eq("bill_id",orderNo);
        return chargingSuccessMapper.selectOne(chargingSuccessPOQueryWrapper);
    }

    @Override
    public ChargingFailPO getFailOrder(String orderNo) {
        QueryWrapper<ChargingFailPO> chargingFailPOQueryWrapper = new QueryWrapper<>();
        chargingFailPOQueryWrapper.eq("bill_id",orderNo);
        return chargingFailMapper.selectOne(chargingFailPOQueryWrapper);
    }

    @Override
    public void saveFailOrder(ChargingFailPO newChargingFailPO) {
        chargingFailMapper.insert(newChargingFailPO);
    }

    @Override
    public void updateDeviceInfo(String orderNo, Integer gunId) {
        QueryWrapper<ChargingSuccessPO> chargingFailPOQueryWrapper = new QueryWrapper<>();
        chargingFailPOQueryWrapper.eq("bill_id",orderNo);
        ChargingSuccessPO chargingSuccessPO = new ChargingSuccessPO();
        chargingSuccessPO.setBillStatus(1);
        chargingSuccessPO.setGunId(gunId);
        chargingSuccessMapper.update(chargingSuccessPO,chargingFailPOQueryWrapper);
    }
}
